package com.veeder.datacollection.config;

import com.veeder.datacollection.exception.ConfigurationException;

public class SerialPortParam {

	public enum Parity {
		None, Odd, Even, Mark, Space
	}

	private String portName;
	private int baudRate;
	private Parity parity;
	private int databits;
	private int stopbits;

	public String getPortName() {
		return portName;
	}

	public void setPortName(String portName) throws ConfigurationException {
		if (portName == null || portName.isEmpty()) {
			throw new ConfigurationException("PortName is null or empty.");
		}
		this.portName = portName;
	}

	public int getBaudRate() {
		return baudRate;
	}

	public void setBaudRate(String baudRate) throws ConfigurationException {
		try {
			this.baudRate = Integer.parseInt(baudRate);
		} catch (Exception e) {
			throw new ConfigurationException("baudRate error", e);
		}
	}

	public Parity getParity() {
		return parity;
	}

	public void setParity(String parity) throws ConfigurationException {
		try {
			int index = Integer.parseInt(parity);
			this.parity = Parity.values()[index];
		} catch (Exception e) {
			throw new ConfigurationException("parity config error", e);
		}
	}

	public int getDatabits() {
		return databits;
	}

	public void setDatabits(String databits) throws ConfigurationException {

		try {
			int val = Integer.parseInt(databits);

			if (val < 5 || val > 8) {
				throw new ConfigurationException("databits must be int between 5-8");
			}

			this.databits = val;
		} catch (NumberFormatException e) {
			throw new ConfigurationException("databits config error", e);
		}

	}

	public int getStopbits() {
		return stopbits;
	}

	public void setStopbits(String stopbits) throws ConfigurationException {
		try {
			int val = Integer.parseInt(stopbits);

			if (val < 1 || val > 3) {
				throw new ConfigurationException("stopbits must be int between 1-3");
			}

			this.stopbits = val;
		} catch (NumberFormatException e) {
			throw new ConfigurationException("databits config error", e);
		}
	}
}
